/**
* Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners. 
* See LICENSE.TXT for license information.
*/

package com.nokia.example.paint.views;

import com.nokia.example.paint.helpers.ImageLoader;
import com.nokia.example.paint.views.components.Button;
import com.nokia.example.paint.views.components.ProjectsButton;
import com.nokia.example.paint.views.components.StartButton;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/*
 * Splash view that is shown when starting the application.
 */
public class Splash
        extends GameCanvas {

    private Image infoImg;
    private Button startBtn;
    private Button projectsBtn;

    public Splash() {
        super(false);
        setFullScreenMode(true);
        ImageLoader il = ImageLoader.getInstance();
        int horizontalCenter = getWidth() / 2;
        int verticalCenter = getHeight() / 2;

        infoImg = il.loadImage("/info_view_small.png");
        startBtn = new StartButton();
        startBtn.setPosition(horizontalCenter - startBtn.getWidth() / 2, verticalCenter + 56);
        projectsBtn = new ProjectsButton();
        projectsBtn.setPosition(horizontalCenter - projectsBtn.getWidth() / 2, verticalCenter + 107);

    }

    protected void render() {
        Graphics g = getGraphics();
        g.drawImage(infoImg, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
        startBtn.render(g);
        projectsBtn.render(g);
        flushGraphics();
    }

    /**
     * Called when the drawable area of the Canvas has been changed.
     * @see javax.microedition.lcdui.Canvas#sizeChanged(int, int)
     * @param w the new width in pixels of the drawable area of the Canvas
     * @param h the new height in pixels of the drawable area of the Canvas
     */
    protected void sizeChanged(int w, int h) {
        render();
    }

    /**
     * Called when canvas is shown.
     * @see javax.microedition.lcdui.Canvas#showNotify()
     */
    protected void showNotify() {
        render();
    }

    /**
     * Handles pointer press.
     * @see javax.microedition.lcdui.Canvas#pointerPressed(int, int)
     * @param x coordinate of press
     * @param y coordinate of press
     */
    protected void pointerPressed(int x, int y) {
        startBtn.pressed(x, y);
        projectsBtn.pressed(x, y);
        render();
    }

    /**
     * Handle pointer release event.
     * @see javax.microedition.lcdui.Canvas#pointerReleased(int, int)
     * @param x coordinate of release
     * @param y coordinate of release
     */
    protected void pointerReleased(int x, int y) {
        startBtn.unpressed(x, y);
        projectsBtn.unpressed(x, y);
        render();
    }
}
